package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-10-22
 * Time:14:23
 */
public class Delopteration implements IOpteration{
    public void work(BookList bookList){
        System.out.println("删除图书！");
        //1.找到你要删除的书
        int currentSize=bookList.getUsedsize();
        if(currentSize==0){
            System.out.println("书架为空，不能删除！");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名：");
        String name=scanner.nextLine();

        int index=-1;//存书
        for (int i = 0; i < currentSize; i++) {
            Book book1=bookList.getbooks(i);
            if(book1.getName().equals(name)){
                index=i;
            }
            break;
        }
        //2.index!=-1,有这本书，再删除，把后面的覆盖到前面
        if(index==-1){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book=bookList.getbooks(i+1);
            bookList.setbooks(i,book);
        }
        System.out.println("删除成功！");
        //当书删除之后，Usedsize也要维持！！
        bookList.setUsedsize(currentSize-1);
    }
}
